package com.cyb.beta.core;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.core.Const;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.plugin.tablebind.SimpleNameStyles;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByActionMethods;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.ehcache.EhCachePlugin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class AppConfig extends JFinalConfig {

    private Routes routes;

    @Override
    public void configConstant(Constants constants) {
        loadPropertyFile("config.properties");

        //开发模式
        constants.setDevMode(getPropertyToBoolean("app.devMode", false));
        constants.setEncoding(Const.DEFAULT_ENCODING);
        constants.setBaseViewPath("/WEB-INF/pages");
    }

    @Override
    public void configRoute(Routes routes) {
        this.routes = routes;
        routes.add(new AutoBindRoutes());
    }

    @Override
    public void configPlugin(Plugins plugins) {
        // 缓存插件
        plugins.add(new EhCachePlugin());
        // shiro
        plugins.add(new ShiroPlugin(this.routes));

        // 数据源插件
        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbc.url"), getProperty("jdbc.username"), getProperty("jdbc.password"), getProperty("jdbc.driver"));
        // 数据库监控
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        druidPlugin.addFilter(new StatFilter());
        plugins.add(druidPlugin);

        //自动绑定
        AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(druidPlugin, SimpleNameStyles.LOWER_UNDERLINE);
        autoTableBindPlugin.setShowSql(getPropertyToBoolean("app.showSql", false));
        autoTableBindPlugin.setDialect(new MysqlDialect());
        plugins.add(autoTableBindPlugin);
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        // 自定义未登陆拦截
//        interceptors.add(new ManagerInterceptor());

        // shiro权限拦截器配置
        interceptors.add(new ShiroInterceptor());

        // 解决session在freemarker中不能取得的问题 获取方法：${session["manager"].username}
        interceptors.add(new SessionInViewInterceptor());

        // 添加事务，对add、save、update和delete自动进行拦截
        interceptors.add(new TxByActionMethods("add", "save", "update", "delete"));
    }

    @Override
    public void configHandler(Handlers handlers) {
        handlers.add(new ContextPathHandler());
        DruidStatViewHandler druidStatViewHandler = new DruidStatViewHandler("/admin/druid", new IDruidStatViewAuth() {
            @Override
            public boolean isPermitted(HttpServletRequest request) {
                HttpSession session = request.getSession(false); // 不存在会话，不新建
//                return (session != null && ((User)session.getAttribute(Constant.SHIRO_LOGIN_USER)).getStr(User.USERNAME).equals(Constant.SYSTEM_ADMINISTRATOR));
                return true;
            }
        });
        handlers.add(druidStatViewHandler);
    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 9000, "/", 10);
    }
}